﻿package gui;

import XML.*;
import MainClassPackage.*;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.jdom2.JDOMException;

public class ChoiseFileName extends javax.swing.JFrame {

    private Object x;
    public ChoiseFileName(Object mainThread) {
        initComponents();
        x=mainThread;
        this.setVisible(true);
        this.setResizable(false);
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        this.setBounds((int)(screenSize.getWidth()/2)-215, (int)(screenSize.getHeight()/2)-35, 430, 70);
    
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        info = new javax.swing.JLabel();
        fileName = new javax.swing.JTextField();
        jButtonOk = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(300, 40));

        info.setText("Выберите имя файла:");

        fileName.setText("например file_name.xml");
        fileName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fileNameActionPerformed(evt);
            }
        });
        fileName.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                fileNameFocusLost(evt);
            }
        });
        fileName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                fileNameKeyPressed(evt);
            }
        });

        jButtonOk.setText("Ok");
        jButtonOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonOkMouseClicked(evt);
            }
        });

        jButtonCancel.setText("Отмена");
        jButtonCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonCancelMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(info)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, 163, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonOk)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonCancel)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(info)
                    .addComponent(fileName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonOk)
                    .addComponent(jButtonCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    private void fileNameActionPerformed(java.awt.event.ActionEvent evt) {                                         
        // TODO add your handling code here:
    }                                        

    private void jButtonOkMouseClicked(java.awt.event.MouseEvent evt) {                                       
        if(!fileName.getText().equalsIgnoreCase("например file_name.xml")){
            String file=fileName.getText();
            String xml=file.substring(file.length()-3, file.length());
            if(xml.equalsIgnoreCase("xml")){
                DataXmlList newJournal=new DataXmlList(file);
                mainClass.enotherJournal=newJournal;
                synchronized(x){
                    x.notifyAll();
                }
                setVisible(false);
                dispose();
            }else JOptionPane.showMessageDialog(null, "Неверно задано имя файла!", "Ошибка", JOptionPane.ERROR_MESSAGE);
        }else JOptionPane.showMessageDialog(null, "Не задано имя файла!", "Ошибка", JOptionPane.ERROR_MESSAGE);
    }                                      

    private void jButtonCancelMouseClicked(java.awt.event.MouseEvent evt) {                                           
        setVisible(false);
        dispose();
    }                                          

    private void fileNameKeyPressed(java.awt.event.KeyEvent evt) {                                    
        if(fileName.getText().equalsIgnoreCase("например file_name.xml")){
            fileName.setText("");
        }
    }                                   

    private void fileNameFocusLost(java.awt.event.FocusEvent evt) {                                   
        if(fileName.getText().equalsIgnoreCase("")){
            fileName.setText("например file_name.xml");
        }
    }                                  

    // Variables declaration - do not modify                     
    private javax.swing.JTextField fileName;
    private javax.swing.JLabel info;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOk;
    // End of variables declaration                   
}
